<?php 
/**
 * Joomla! 1.5 component arts_curriculum
 * @package    arts_curriculum
 * @subpackage
 * @author     Chris Borkowski {@link http://www.guggenheim.org}
 * @author     Created on 10-October-2009
 * @author     Modified on 10-January-2010
 */
// no direct access

defined( '_JEXEC' ) or die( 'Restricted access' );
jimport('joomla.application.component.model');

		/**
		* Arts_curriculum Component Item Model
		* @package Arts_curriculum
		*/
		class Arts_curriculumModelItem extends JModel
		{
		/**
		* Item id
		*
		* @var int
		*/
		var $_id = null;
		/**
		* Item data
		*
		* @var array
		*/
		var $_data = null;
		
			
			/**
			* Constructor
			*
			* @since 1.5
			*/
			function __construct()
			{
			parent::__construct();
			$id = JRequest::getVar('id', 0, '', 'int');
			$this->setId((int)$id);
			}
		
		
			/**
			* Method to set the item identifier
			*
			* @access public
			* @param int Item identifier
			*/
			function setId($id)
			{
			// Set item id and wipe data
			$this->_id = $id;
			$this->_data = null;
			}
		
		
			/**
			* Method to get a item
			*
			* @since 1.5
			*/
			function &getData()
			{
			// Load the item data
			if ($this->_loadData())
			{
			// Initialize some variables
			$user = &JFactory::getUser();
			// Make sure the item is published
			if (!$this->_data->published) {
			JError::raiseError(404, JText::_("Resource Not Found"));
			return false;
			}
			// Check to see if the category is published
			if (!$this->_data->cat_pub) {
			JError::raiseError( 404, JText::_("Resource Not Found") );
			return;
			}
			// Check whether category access level allows access
			if ($this->_data->cat_access > $user->get('aid', 0)) {
			JError::raiseError( 403, JText::_('ALERTNOTAUTH') );
			return;
			}
			}
			//else $this->_initData();
			return $this->_data;
			}
		
			/**
			* Method to increment the hit counter for the item
			*
			* @access public
			* @return boolean True on success
			* @since 1.5
			*/
			function hit()
			{
			global $mainframe;
			
			if ($this->_id)
			{
			$item = & $this->getTable();
			$item->hit($this->_id);
			return true;
			}
			return false;
			}
		
			/**
			* Method to load content item data
			*
			* @access private
			* @return boolean True on success
			*/
			function _loadData()
			{
			// Lets load the content if it doesn't already exist
			if (empty($this->_data))
			{
			$query = 'SELECT w.*, cc.title AS category, ' .
			' cc.published AS cat_pub, cc.access AS cat_access, cc.id AS cat_id,'.
			' v.main_body as v_main_body, v.additional_resources as v_additional, v.pdf as v_pdf'.
			' FROM #__arts_curriculum AS w' .
			' LEFT JOIN #__categories AS cc ON cc.id = w.catid' .
			' LEFT JOIN jos_arts_curriculum_vocab AS v ON v.catid = w.catid'.
			' WHERE w.id = '. (int) $this->_id;
			$this->_db->setQuery($query);
			$this->_data = $this->_db->loadObject();
			return (boolean) $this->_data;
			}
			return true;
			}
}